/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.cykj.pos.util;

//import org.apache.commons.lang.StringUtils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.UUID;

/**
 * 封装各种生成唯一性ID算法的工具类.
 *
 * @author ThinkGem
 * @version 2013-01-15
 */
@Slf4j
public class IdGen {

    private static SecureRandom random = new SecureRandom();

    /**
     * 封装JDK自带的UUID, 通过Random数字生成, 中间无-分割.
     */
    public static String uuid() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 使用SecureRandom随机生成Long.
     */
    public static long randomLong() {
        return Math.abs(random.nextLong());
    }

    public static String genBatchNo() {
        int max = 1000000;
        int min = 100000;
        int s = random.nextInt(max) % (max - min + 1) + min;
        return new SimpleDateFormat("yyyyMMddHHmmss").format(Calendar
                .getInstance().getTime())
                + StringUtils.leftPad(String.valueOf(s), 5, "0");
    }

    public static String genBatchOrderNo() {
        int max = 1000000;
        int min = 100000;
        int s = random.nextInt(max) % (max - min + 1) + min;
        return new SimpleDateFormat("yyyyMMddHHmmssSSS").format(Calendar
                .getInstance().getTime())
                + StringUtils.leftPad(String.valueOf(s), 7, "0");
    }

    public static String genInvoiceApplyNo() {
        int max = 1000000;
        int min = 100000;
        int s = random.nextInt(max) % (max - min + 1) + min;
        return new SimpleDateFormat("yyyyMMddHHmmssSSS").format(Calendar
                .getInstance().getTime())
                + StringUtils.leftPad(String.valueOf(s), 7, "0");
    }

    public static void main(String[] args) {
        log.info(genBatchNo());
    }

}
